package com.zky.DAO.impl;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.zky.common.HibernateSessionFactory;
import com.zky.entity.Users1;



public class User1DaoImp extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
	
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		String username = request.getParameter("username");
		List<Users1> list=new ArrayList<Users1>();
		
		String msg = "";
		 Session session1 = HibernateSessionFactory.getSession();
		 Transaction transaction = session1.beginTransaction();	
		   try{
			  String hql="from Users1 as muser where muser.username=?";
			  Query query=session1.createQuery(hql);
			  query.setString(0, username);
			   list=query.list();
			   String msgg="";
				if (null == list || list.size() == 0) {
				msgg="";
			    	System.out.print("");
			    }
				else{
				msgg="用户名已存在";
					System.out.print(msgg);
				}
				
			   transaction.commit();
			   out.write(msgg);
			   }catch(Exception e){
			    e.printStackTrace();
			   }finally{
			    session1.close();
		
	}
	
		
		out.flush();
		out.close();
	}

}
